package com.tanxin.bill.common.weight;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.tanxin.bill.R;

@SuppressLint("ValidFragment")
public class AddCustomerDialog extends DialogFragment {
    private Context context;
    private Customer customer;
    public AddCustomerDialog(Context context, Customer customer) {
        this.context = context;
        this.customer = customer;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.customer_add_dialog,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("添加客户").setView(view);
        final Dialog dialog= builder.create();
        final EditText edName = view.findViewById(R.id.ed_name);
        final EditText edPhone = view.findViewById(R.id.ed_phone);
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = edName.getText().toString().trim();
                String phone = edPhone.getText().toString().trim();
                if (StringUtils.isEmpty(name)){
                    Toast.makeText(context, "请输入姓名", Toast.LENGTH_SHORT).show();
                }else if (!RegexUtils.isMobileExact(phone)){
                    Toast.makeText(context, "请输入正确手机号", Toast.LENGTH_SHORT).show();
                }else {
                    customer.getInfo(name,phone);
                    dialog.dismiss();
                }

            }
        });

        return dialog;
    }

    public interface Customer{
        void getInfo(String name,String phone);
    }
}
